package com.sam.kafka.consumer;

import com.sam.kafka.entity.User;
import com.sam.kafka.serialization.UserDerSerializer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: wm.xue
 * @date: 2021/11/9 12:54
 * @description:
 */
public class _03_Consumer_MyDerSerializer {

    public static void main(String[] args) {
        Map<String, Object> configs = new HashMap<>();
        configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "10.100.17.117:9092");
        configs.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        configs.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, UserDerSerializer.class);

        configs.put(ConsumerConfig.GROUP_ID_CONFIG,"_03_Consumer_MyDerSerializer");
        configs.put(ConsumerConfig.CLIENT_ID_CONFIG, "_03_Consumer_MyDerSerializer");
        configs.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");


        KafkaConsumer<String, User> consumer = new KafkaConsumer<String, User>(configs);
        consumer.subscribe(Arrays.asList("_01_serialize"));

        ConsumerRecords<String, User> records = consumer.poll(Duration.ofSeconds(10));
        records.forEach(record -> {
            System.err.println(record.value());
        });
        consumer.close();
    }
}
